package com.ricervcvcer.user.service.impl;

import cn.hutool.core.codec.Base64;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ricervcvcer.common.dto.UserDTO;
import com.ricervcvcer.user.entity.SysPermis;
import com.ricervcvcer.user.entity.SysRight;
import com.ricervcvcer.user.mapper.SysPermisMapper;
import com.ricervcvcer.user.mapper.SysRightMapper;
import com.ricervcvcer.user.service.ISysRightService;
import com.ricervcvcer.user.util.RicervcvcerConstant;
import com.ricervcvcer.user.vo.MenuNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author moge
 * @since 2022-01-10
 */
@Service
@Slf4j
public class SysRightServiceImpl extends ServiceImpl<SysRightMapper, SysRight> implements ISysRightService {
    @Resource
    private SysRightMapper rightMapper;
    @Resource
    private SysPermisMapper permisMapper;

    @Override
    public List<MenuNode> menus(HttpServletRequest request) {
        String token = request.getHeader("jwt-token");
        String decode = Base64.decodeStr(token);
        Object parse = JSON.parse(decode);
        JSONObject jsonObject = parse != null ? (JSONObject) parse : null;
        String principal = (String) jsonObject.get("principal");
        String userDTO = Base64.decodeStr(principal);
        UserDTO userDTO1 = JSONObject.parseObject(userDTO, UserDTO.class);
        log.info("userDTO1:{}", userDTO1);
//        UserDTO userDTO1 = (UserDTO) JSON.parse(userDTO);

        List<MenuNode> menus = rightMapper.selectMenu("0", userDTO1.getUsrId());
        menus.forEach(per -> {
            List<MenuNode> menus2 = rightMapper.selectMenu(per.getRightCode(), userDTO1.getUsrId());
            menus2.forEach(per2 -> {
                List<MenuNode> menus3 = rightMapper.selectMenu(per2.getRightCode(), userDTO1.getUsrId());
                per2.setMenus(menus3);
            });
            per.setMenus(menus2);
        });
        return menus;
    }

    @Override
    public List<MenuNode> menusPermis() {
        List<MenuNode> menus = rightMapper.selectMenu2("0");
        menus.forEach(per -> {
            // 查询子菜单
            List<MenuNode> menus2 = rightMapper.selectMenu2(per.getRightCode());
            menus2.forEach(per2 -> {
                List<MenuNode> menus3 = rightMapper.selectMenu2(per2.getRightCode());
                if(!per2.getRightType().equals(RicervcvcerConstant.RIGHT_TYPE_FOLDER)) {
                    convet(per2);
                }
                if(!Objects.isNull(menus3) && menus3.size()!=0) {
                    menus3.forEach(per3 -> {
                        if (!per3.getRightType().equals("Folder")) {
                            convet(per3);
                        }
                    });
                    per2.setMenus(menus3);
                }
            });
            per.setMenus(menus2);
        });
        return menus;
    }

    @Override
    public List<MenuNode> menus() {
        List<MenuNode> menus = rightMapper.selectMenu2("0");
        menus.forEach(per -> {
            // 查询子菜单
            List<MenuNode> menus2 = rightMapper.selectMenu2(per.getRightCode());
            menus2.forEach(per2 -> {
                List<MenuNode> menus3 = rightMapper.selectMenu2(per2.getRightCode());
                per2.setMenus(menus3);
            });
            per.setMenus(menus2);
        });
        return menus;
    }

    @Override
    public List<MenuNode> folders() {
        List<MenuNode> menus = rightMapper.selectMenu2("0");
        menus.stream().filter(item -> {
            return item.getRightType().equals("Folder");
        }).forEach(per -> {
            List<MenuNode> menus2 = rightMapper.selectMenu2(per.getRightCode());
            if (menus2.size() != 0) {
                per.setMenus(menus2.stream().filter(item -> {
                    return item.getRightType().equals("Folder");
                }).map(per2 -> {
                    List<MenuNode> menus3 = rightMapper.selectMenu2(per2.getRightCode());
                    if (menus3.size() != 0) {
                        per2.setMenus(menus3.stream().filter(per3 -> {
                            return per3.getRightType().equals("Folder");
                        }).collect(Collectors.toList()));
                    }
                    return per2;
                }).collect(Collectors.toList()));
            }
        });
        return menus;
    }

    public void convet(MenuNode menuNode){
        // 查询子菜单下的权限
        QueryWrapper qw = new QueryWrapper();
        qw.eq("per_right_code", menuNode.getRightCode());
        List<SysPermis> permis = permisMapper.selectList(qw);
        // 查询当前菜单下的权限
        List<MenuNode> request = permis.stream().map(i -> {
            MenuNode m = new MenuNode();
            m.setRightCode(i.getPerId().toString());
            m.setRightText(i.getPerText());
            m.setRightType(RicervcvcerConstant.PERMIS_REQUEST);
            m.setRightUrl(i.getPerUrl());
            m.setRightTip(i.getPerContent());
            m.setRightParent(menuNode.getRightCode());
            return m;
        }).collect(Collectors.toList());
        menuNode.setMenus(request);
    }
}
